﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    string ime, prez, mail, pass, karticka;
    int broj;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IspolniGrid();
            Admin_Grid();
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
            string sqlString = "SELECT ime FROM Info";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);
            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    lblNaslov.Text = citac["ime"].ToString();
                }
                citac.Close();
            }
            catch (Exception err)
            {
                lblGreska.Text = err.Message;
            }
            finally
            {
                konekcija.Close();
            }

        }
        
    }
    protected void IspolniGrid()
    {
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        string sqlString = "SELECT * FROM Klient";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        SqlDataAdapter adapter = new SqlDataAdapter(komanda);
        DataSet ds = new DataSet();
        try
        {
            konekcija.Open();
            adapter.Fill(ds, "Klienti");
            gvSporedba.DataSource = ds;
            gvSporedba.DataBind();
            ViewState["dataset"] = ds;
        }
        catch (Exception err)
        {
            lblGreska.Text = err.Message;
        }
        finally
        {
            konekcija.Close();
        }
    }
    protected void btnLogirajse_Click(object sender, EventArgs e)
    {
        proverka_admin();
        for (int i = 0; i < gvSporedba.Rows.Count; i++)
        {
            if (gvSporedba.Rows[i].Cells[0].Text == username.Text)
            {
                if (gvSporedba.Rows[i].Cells[1].Text == password.Text)
                {
                    FormsAuthenticationTicket ticket;
                    string cookiestr;
                    HttpCookie cookie;
                    ticket = new FormsAuthenticationTicket(1,
                        username.Text,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(10),
                        true,
                        GetUserId(username.Text).ToString());
                    // enkruptiraj go tiketot (text za kolaceto)
                    Session["korisnik"] = username.Text;
                    cookiestr = FormsAuthentication.Encrypt(ticket);
                    // kreiraj kolace
                    cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
                    cookie.Expires = ticket.Expiration;
                    cookie.Path = FormsAuthentication.FormsCookiePath;
                    Response.Cookies.Add(cookie);
                    Response.Redirect("Doma.aspx");
                }
               
            }
        }
    }
    private int GetUserId(string username)
    {
        return 1;
    }
    protected void d_Click(object sender, EventArgs e)
    {
        Wizard1.Visible = true;
    }

    protected void greska1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txtIme.Text.Length > 14)
            args.IsValid = false;
    }
    protected void greska2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txtPrezime.Text.Length > 14)
            args.IsValid = false;
    }
    protected void greska3_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if(proverka())
            args.IsValid = false;

    }
    bool proverka()
    {
        username.Text = txtMail.Text;
        for (int i = 0; i < gvSporedba.Rows.Count ; i++)
        {
            if (txtMail.Text == gvSporedba.Rows[i].Cells[0].Text)
            {

                Session["email"] = 1;
                return true;
            }

        }
        return false;
    }
    protected void greska4_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (txtKreditnaKarticka.Text.Length != 16)
            args.IsValid = false;
    }
  
    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        SqlConnection konecija = new SqlConnection();
        konecija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        ime = txtIme.Text;
        prez = txtPrezime.Text;
        pass =txtLozinka.Text;
        karticka = txtKreditnaKarticka.Text;
        mail = txtMail.Text;
        broj = gvSporedba.Rows.Count+1;
        string sqlString = "INSERT INTO Klient values('"+broj+"','" + ime + "','" + prez + "','" + karticka + "','" + mail + "','" + pass + "')";
        SqlCommand komanda = new SqlCommand(sqlString, konecija);

        try
        {
            if (Session["mail"] == null)
            {
                konecija.Open();
                komanda.ExecuteNonQuery();
                Wizard1.Visible = false;
                Response.Redirect("Login.aspx");
            }
        }
        catch (Exception err)
        {
            lblGreska.Text = err.Message;
        }
        finally
        {
            konecija.Close();
        }
        
    }
    protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        lblIme.Text = txtIme.Text;
        lblPrezime.Text = txtPrezime.Text;
        lblMail.Text = txtMail.Text;
        lblKarticka.Text = txtKreditnaKarticka.Text;
    }
    protected void Menu2_MenuItemClick(object sender, MenuEventArgs e)
    {
        if (Menu2.SelectedValue == "Маици")
            Response.Redirect("za_mazi.aspx?tip=maici");
        if (Menu2.SelectedValue == "Патики")
            Response.Redirect("za_mazi.aspx?tip=patiki");
        if (Menu2.SelectedValue == "Панталони")
            Response.Redirect("za_mazi.aspx?tip=pantaloni");
        if (Menu2.SelectedValue == "Фустани")
            Response.Redirect("za_zeni.aspx?tip=fustani");
        if (Menu2.SelectedValue == "Штикли")
            Response.Redirect("za_zeni.aspx?tip=stikli");
        if (Menu2.SelectedValue == "Капи")
            Response.Redirect("za_zeni.aspx?tip=kapi");
        if (Menu2.SelectedValue == "Облека за мажи")
            Response.Redirect("za_mazi.aspx?tip=mazi");
        if (Menu2.SelectedValue == "Облека за жени")
            Response.Redirect("za_zeni.aspx?tip=zeni");
    }
    protected void Admin_Grid()
    {
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        string sqlString = "SELECT * FROM Admin";
        SqlCommand komanda = new SqlCommand(sqlString, konekcija);
        SqlDataAdapter adapter = new SqlDataAdapter(komanda);
        DataSet ds = new DataSet();
        try
        {
            konekcija.Open();
            adapter.Fill(ds, "admin");
            gvAdmin.DataSource = ds;
            gvAdmin.DataBind();
            ViewState["dataset"] = ds;
        }
        catch (Exception err)
        {
            lblGreska.Text = err.Message;
        }
        finally
        {
            konekcija.Close();
        }

    }
    protected void proverka_admin()
    {
        for (int i = 0; i < gvAdmin.Rows.Count; i++)
        {
            if (gvAdmin.Rows[i].Cells[1].Text == username.Text)
            {
                if (gvAdmin.Rows[i].Cells[2].Text == password.Text)
                {
                    FormsAuthenticationTicket ticket;
                    string cookiestr;
                    HttpCookie cookie;
                    ticket = new FormsAuthenticationTicket(1,
                        username.Text,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30),
                        true,
                        GetUserId(username.Text).ToString());
                    // enkruptiraj go tiketot (text za kolaceto)
                    Session["korisnik"] = username.Text;
                    cookiestr = FormsAuthentication.Encrypt(ticket);
                    // kreiraj kolace
                    cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
                    cookie.Expires = ticket.Expiration;
                    cookie.Path = FormsAuthentication.FormsCookiePath;
                    Response.Cookies.Add(cookie);
                    Response.Redirect("Admin.aspx");
                }
            }
        }
    }
}